home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / mkutil21.zip / SRC / UTIL / DJ_LD.C next >
C/C++ Source or Header  |  1994-08-23  |  5KB  |  198 lines

  1. /****************************************************************************
  2. *
  3. *                    Copyright (C) 1994 SciTech Software.
  4. *                            All rights reserved.
  5. *
  6. * Filename:        $RCSfile: dj_ld.c $
  7. * Version:        $Revision: 1.2 $
  8. *
  9. * Language:        Borland C++ 3.1 (not tested with anything else)
  10. * Environment:    MSDOS
  11. *
  12. * Description:    Simple program to generate a .EXE file with GO32 bound to
  13. *                it directly. You call it with the following syntax:
  14. *
  15. *                    gcc_ld [options] <mainfile>.exe [objects/libs | @<rspfile>]
  16. *
  17. *               It will issue the following commands to build the resulting
  18. *                .exe file:
  19. *
  20. *                    gcc [options] [objects/libs] -o mainfile
  21. *                 *  strip mainfile
  22. *                    coff2exe mainfile
  23. *                 *  del mainfile
  24. *
  25. *                Notice that it generates the final file <mainfile>.exe as
  26. *                output. If the -g debugging flag is specified on the
  27. *                command line, then the 'strip mainfile' command will not
  28. *                be executed, and neither will the del mainfile command.
  29. *
  30. *                Also note that libraries are specified on the command
  31. *                in the normal DOS style format (mylib.a) rather than the
  32. *                Unix format (-lmylib, which looks for the file libmylib.a).
  33. *                Since GCC will only search the current directory for a
  34. *                library named 'mylib.a', we handle searching the path
  35. *                specified in the LIBRARY_PATH environment variable and
  36. *                provide the full pathname to the library file to link with.
  37. *
  38. * $Id: dj_ld.c 1.2 1994/08/22 11:11:54 kjb Exp $
  39. *
  40. ****************************************************************************/
  41.  
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <dir.h>
  46. #include <io.h>
  47. #include <process.h>
  48.  
  49. #define    true    1
  50. #define    false    0
  51. #define    BUFSIZE    4096
  52. #define    MAXLIB    20
  53.  
  54. int     debug = false,foundmain = false;
  55. char    mainfile[40];
  56. char    libenv[255];
  57. char    *libpath[MAXLIB];
  58.  
  59. void addCommand(FILE *rspfile, char *command, int len)
  60. {
  61.     int        i;
  62.     char     path[255],*p;
  63.  
  64.     if (command[len-1] == 'a' && command[len-2] == '.') {
  65.         i = 0;
  66.         while (libpath[i] != NULL) {
  67.             strcpy(path,libpath[i]);
  68.             strcat(path,"/");
  69.             strcat(path,command);
  70.             if (access(path,4) == 0)
  71.                 break;
  72.             i++;
  73.             }
  74.         if (libpath[i])
  75.             fprintf(rspfile, path);
  76.         else fprintf(rspfile, command);
  77.         fprintf(rspfile," ");
  78.         }
  79.     else {
  80.         fprintf(rspfile, command);
  81.         fprintf(rspfile, " ");
  82.         }
  83. }
  84.  
  85. void processCommand(FILE *rspfile, char *command)
  86. {
  87.     int    len = strlen(command);
  88.  
  89.     if (!foundmain && command[0] != '-') {
  90.         strcpy(mainfile, command);
  91.         if (len < 3) {
  92.             fprintf(stderr, "Expecting name of .exe file!\n");
  93.             exit(-1);
  94.             }
  95.         if (mainfile[len-4] == '.')
  96.             mainfile[len-4] = '\0';
  97.         foundmain = true;
  98.         }
  99.     else if (foundmain)
  100.         addCommand(rspfile, command, len);
  101.     else {
  102.         fprintf(rspfile, command);
  103.         fprintf(rspfile, " ");
  104.         }
  105.     if (command[0] == '-' && command[1] == 'g')
  106.         debug = true;
  107. }
  108.  
  109. void processRspFile(FILE *rspfile, const char *filename)
  110. {
  111.     FILE    *localrsp = fopen(filename+1, "rt");
  112.     char    *buffer = malloc(BUFSIZE), *p;
  113.  
  114.     if (!buffer) {
  115.         printf("Out of memory!\n");
  116.         exit(-1);
  117.         }
  118.     if (!localrsp) {
  119.         printf("Unable to open response file %s!\n", filename);
  120.         exit(-1);
  121.         }
  122.  
  123.     while (fgets(buffer, BUFSIZE, localrsp)) {
  124.         p = strtok(buffer, " ");
  125.         while (p) {
  126.             processCommand(rspfile, p);
  127.             p = strtok(NULL, " ");
  128.             }
  129.         }
  130.  
  131.     fclose(localrsp);
  132.     free(buffer);
  133. }
  134.  
  135. int main(int argc, char *argv[])
  136. {
  137.     int        status,i;
  138.     char    *rspfilename,command[80],*p;
  139.     FILE    *rspfile;
  140.  
  141.     if (argc < 2) {
  142.         fprintf(stderr, "Usage: dj_ld [options] <mainfile>.exe [objects/libs | @<rspfile>]\n");
  143.         return -1;
  144.         }
  145.  
  146.     /* Build the response file to call GCC with */
  147.  
  148.     rspfilename = mktemp("TXXXXXX");
  149.     if ((rspfile = fopen(rspfilename, "wt")) == NULL) {
  150.         printf("Unable to open temporary file!\n");
  151.         exit(1);
  152.         }
  153.  
  154.     /* Build the list of library directories from the environment variable */
  155.  
  156.     i = 0;
  157.     if ((p = getenv("LIBRARY_PATH")) != NULL) {
  158.         strcpy(libenv,p);
  159.         p = strtok(libenv,";");
  160.         while (p) {
  161.             libpath[i++] = p;
  162.             p = strtok(NULL, ";");
  163.             }
  164.         }
  165.     libpath[i] = NULL;
  166.  
  167.     /* Process all commands on command line */
  168.  
  169.     for (i = 1; i < argc; i++) {
  170.         if (foundmain && argv[i][0] == '@')
  171.             processRspFile(rspfile, argv[i]);
  172.         else processCommand(rspfile, argv[i]);
  173.         }
  174.  
  175.     fprintf(rspfile, "-o %s\n", mainfile);
  176.     fclose(rspfile);
  177.  
  178.     printf("gcc @%s\n", rspfilename);
  179.     strcpy(command, "@");
  180.     strcat(command, rspfilename);
  181.     status = spawnlp(P_WAIT, "gcc", "gcc", command, NULL);
  182.     unlink(rspfilename);
  183.  
  184.     if (status != 0) return status;
  185.     if (!debug) {
  186.         printf("strip %s\n", mainfile);
  187.         spawnlp(P_WAIT, "strip", "strip", mainfile, NULL);
  188.         }
  189.     printf("coff2exe %s\n", mainfile);
  190.     spawnlp(P_WAIT, "coff2exe", "coff2exe", mainfile, NULL);
  191.     if (!debug) {
  192.         printf("rm %s\n", mainfile);
  193.         spawnlp(P_WAIT, "rm", "rm", mainfile, NULL);
  194.         }
  195.  
  196.     return status;
  197. }
  198.